/**
 * Created by wangxin on 2017-9-26 10:02:01.
 */

'use strict';
const page = {
    url: {
        // loadSignUI: Dolphin.path.mengkePath + '/api/bonus/config?token='+contextData.data.gId,
        // sign: Dolphin.path.mengkePath + '/api/bonus/sign?token='+contextData.data.gId,
        sign: Dolphin.path.contextPath + '/work/sign/'+contextData.data.gId,
        loadSignUI: Dolphin.path.contextPath + '/work/bonus/config/'+contextData.data.gId,
    },
    mainImg: $('.mainImg'),
    couponIcon: $('.couponIcon'),
    recordIcon: $('.recordIcon'),
    container: $('#container'),
    pageFooterLogo: $('.pageFooter img'),
};

page.init = function () {
    page.initElement();
    page.initEvent();
};

page.initElement = function () {
    let thisPage = this;
    Dolphin.ajax({
        url: thisPage.url.loadSignUI,
        type: "get",
        forceUrl: 'true',
        onSuccess: function (data) {
            thisPage.renderMenuList(data.data);
            thisPage.renderActivityList(data.data);
            thisPage.renderConfig(data.data);
        },
        onError: function (e) {
            alert('网络出错啦，请您重新加载');
        }
    });
};

page.initEvent = function () {
    let thisPage = this;
    let userInfo = {
        openId: contextData.userData.openid,
        unionId: contextData.userData.unionid,
        name: contextData.userData.vipName,
        cardCode: contextData.userData.cardCode,
        mobile: contextData.userData.mobile,
        nickName: contextData.userData.nickname,
    };
    $('#mainImg').click(function(){
        Dolphin.ajax({
            url: thisPage.url.sign,
            type: "post",
            forceUrl: true,
            async: false,
            data: JSON.stringify(userInfo),
            onSuccess: function (data) {
                // let message = data.message;
                // if(data.data.point){
                //     message += '　积分+' + data.data.point
                // }
                // Dolphin.alert(message);
                thisPage.alert('签到成功', '恭喜您获得 ' + data.data.point + ' 积分');
            },
            onError: function (e) {
                // Dolphin.alert(e.message || '网络出错啦，请您重新加载');
                thisPage.alert('签到失败', e.message || '网络出错啦，请您重新加载');
            }
        });
    });

    $('.signInfoModel, .signInfoButton button, .signInfoClose').click(function(){
        $('.signInfo').hide();
    })
};

page.renderConfig = function (data) {
    let mainColor = data['mainColor'] || 'rgb(227, 79, 116)';
    if(data.mainImg){
        this.mainImg.attr('src',Dolphin.path.mengkeUploadPath + data.mainImg);
    }else{
        this.mainImg.attr('src',Dolphin.path.mengkePublicPath + '/custom/images/bonus/main.gif');
    }
    if(data.couponIcon){
        this.couponIcon.attr('src',Dolphin.path.mengkeUploadPath + data.couponIcon);
    }else{
        this.couponIcon.attr('src',Dolphin.path.mengkePublicPath + '/custom/images/bonus/coupon.png');
    }
    if(data.recordIcon){
        this.recordIcon.attr('src',Dolphin.path.mengkeUploadPath + data.recordIcon);
    }else{
        this.recordIcon.attr('src',Dolphin.path.mengkePublicPath + '/custom/images/bonus/record.png');
    }
    this.container.css({
        background: data.bgColor || '#ffffff',
    });
    $('.menuLink').css({
        color: data['fontColor'] || '#000000',
    });
    $('.signInfo .signInfoPanel .signInfoButton button').css({
        background: mainColor,
    });
    $('.signInfo .signInfoPanel .signInfoTitle').css({
        color: mainColor,
    });
    $('.pageFooter').css('background', data['footerBg'] || '#eee');
    if(data['footerLogo']){
        this.pageFooterLogo.attr('src',Dolphin.path.mengkeUploadPath + data['footerLogo']);
    }else{
        this.pageFooterLogo.attr('src',Dolphin.path.mengkePublicPath + '/custom/images/icon/logo.png');
    }
};
page.renderMenuList = function(data){
    let thisPage = this;
    let panel = $('#menuPanel');
    panel.find('.customer').remove();
    data.menus.forEach(function(menu){
        let div = $('<div>').appendTo(panel);
        let link = $('<a class="menuLink">').attr('href', menu.link).appendTo(div);
        let block = $('<div class="menuBlock">').appendTo(link);
        let img = $('<img>').attr('src', Dolphin.path.mengkeUploadPath + menu.img).appendTo(block);
        $('<div>').html(menu.name).appendTo(block);
    });
};
page.renderActivityList = function(data){
    let thisPage = this;
    let panel = $('#activityPanel');
    panel.empty();
    data['activity'].forEach(function(activity){
        let div = $('<div class="activityBlock">').appendTo(panel);
        let link = $('<a class="link">').attr('href', activity.link).appendTo(div);
        let img = $('<img>').attr('src', Dolphin.path.mengkeUploadPath + activity.img).appendTo(link);
    });
};
page.alert = function(title, message){
    $('.signInfoTitle').html(title);
    $('.signInfoContent').html(message);
    $('.signInfo').show();
};

$(function () {
    page.init();
});




